﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace DataBaseUtilHelpV2
{
    public class DataBaseToolV2
    {
        /// <summary>
        /// 创建Datatable，规范：列名|列类型,列名|列类型,列名|列类型
        /// 举例：CustomeName|string,Gender|bool,Address
        /// </summary>
        /// <param name="columnsInfo"></param>
        /// <returns></returns>
        public static DataTable CreateTable(string columnsInfo)
        {
            try
            {
                if (string.IsNullOrEmpty(columnsInfo))
                    throw new ArgumentNullException("columnsInfo");
                DataTable _dtNew = new DataTable();
                string[] _columnsList = columnsInfo.Split(',');
                string _columnName;
                string _columnType;
                string[] _singleColumnInfo;
                foreach (string s in _columnsList)
                {
                    _singleColumnInfo = s.Split('|');
                    _columnName = _singleColumnInfo[0];
                    if (_singleColumnInfo.Length == 2)
                    {
                        _columnType = _singleColumnInfo[1];
                        _dtNew.Columns.Add(new DataColumn(_columnName, Type.GetType(HandleColType(_columnType))));
                    }
                    else
                    {
                        _dtNew.Columns.Add(new DataColumn(_columnName));
                    }
                }
                return _dtNew;
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("CreateTable Exception:{0}", ex.Message.Trim()));
            }
        }
        private static string HandleColType(string columnType)
        {

            try
            {
                string _currentType = string.Empty;
                switch (columnType.ToLower())
                {
                    case "int":
                        _currentType = "System.Int32";
                        break;
                    case "string":
                        _currentType = "System.String";
                        break;
                    case "decimal":
                        _currentType = "System.Decimal";
                        break;
                    case "double":
                        _currentType = "System.Double";
                        break;
                    case "dateTime":
                        _currentType = "System.DateTime";
                        break;
                    case "bool":
                        _currentType = "System.Boolean";
                        break;
                    case "image":
                        _currentType = "System.Byte[]";
                        break;
                    case "object":
                        _currentType = "System.Object";
                        break;
                    default:
                        _currentType = "System.String";
                        break;
                }
                return _currentType;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
